import { Module, Store, ActionContext } from "vuex";
import { RootStateType } from "../interface";

interface TestStore {
	items: Array<any>;
}
const store: Module<TestStore, RootStateType> = {
	namespaced: true,
	state: () => {
		return {
			items: [],
		};
	},
	getters: {
		itemsCount: (state: TestStore) => {
			return state.items.length;
		},
		filterItems: (state: TestStore): ((id: number) => any[]) => {
			return id => {
				return state.items.filter(item => item.id === id);
			};
		},
	},
	actions: {
		ADD_ITEM: (injectee: ActionContext<TestStore, RootStateType>, payload?: any) => {
			injectee.commit("pushItem", payload);
		},
		POP_ITEM: (injectee: ActionContext<TestStore, RootStateType>, payload?: any) => {
			injectee.commit("popItem", payload);
		},
	},
	mutations: {
		pushItem: (state: TestStore, payload?: any) => {
			if (payload !== undefined) {
				state.items.push(payload);
			}
		},
		popItem: (state: TestStore, payload?: any) => {
			state.items.pop();
		},
	},
	modules: {},
};

export default store;
